import 'package:any_reader/public.dart';

import '../provider/export.dart';
import 'export.dart';

class SearchBookModel extends ViewStateRefreshListModel {
  ThemeModel themeModel;
  SearchBookModel(this.themeModel);

  List<Rule> rules = [];
  List<Retrieval> hot = [];
  int ruleIndex = 0;
  String searchKey = "";
  int pageSize = 20;
  bool finish = false;
  @override
  initData() async {
    // TODO: implement iniData
    FlutterStatusbarManager.setColor(themeModel.customThemeData.mainColor);
    setBusy(true);
    if (rules.length <= 0) {
      List<Rule> res = await DatabaseHelper().getAllRule();
      rules = res;
      if (res.length <= 0) {
        res = await RulesService.findAll();
        rules = res;
        DatabaseHelper().insertRules(res);
      }
    }
    hot = await RetrievalService.getHot();
    setBusy(false);
    notifyListeners();
    RulesService.findAll().then((List<Rule> res) {
      rules = res;
      DatabaseHelper().insertRules(res);
    });
  }

  @override
  refresh({bool init = false}) {
    // TODO: implement refresh
    setBusy(true);
    super.refresh(init: true);
  }

  @override
  Future<List> loadData({int pageNum = 1}) async {
    String rule = rules[ruleIndex].reptileTypeId;
    List<Book> list = await BookService.search(searchKey, rule);
    if (list.length < this.pageSize) {
      this.finish = true;
    }
    return list;
  }
}
